package com.ship.boats.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ship.boats.domain.SpBoatsReceivingStation;
import com.ship.boats.domain.SpBoatsWeather;
import com.ship.boats.domain.dto.QueryReceivStationRq;
import com.ship.boats.domain.dto.SavePortInfoDto;
import com.ship.boats.domain.vo.QueryBoatsReceivingStationInfo;
import com.ship.boats.mapper.BoatsReceivingStationMapper;
import com.ship.boats.service.BoatsReceivingStationService;
import com.ship.boats.service.BoatsWeatherService;
import com.ship.boats.service.PortInformationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <b>BoatsReceivingStationServiceImpl</b>
 *
 * @description: BoatsReceivingStationServiceImpl <br>
 * @date: 2024/4/16 16:57 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class BoatsReceivingStationServiceImpl extends ServiceImpl<BoatsReceivingStationMapper, SpBoatsReceivingStation> implements BoatsReceivingStationService {
    private static final Logger logger = LoggerFactory.getLogger(BoatsReceivingStationServiceImpl.class);

    @Autowired
    private PortInformationService portInformationService;

    @Override
    public List<SpBoatsReceivingStation> queryReceivingStationList() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enable","1");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public PageInfo<SpBoatsReceivingStation> queryBoatsReceivingStationList(QueryReceivStationRq rq) {
        PageHelper.startPage(rq.getPageNum(),rq.getPageSize());
        List<SpBoatsReceivingStation> boatsList = baseMapper.queryBoatsReceivingStationList(rq);
        PageInfo<SpBoatsReceivingStation> pageInfo = new PageInfo<>(boatsList);
        return pageInfo;
    }

    @Override
    public List<String> queryBoatsReceivintStationByType(String type) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enable","1");
        List<SpBoatsReceivingStation> receivingStationList = baseMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(receivingStationList)){
            if ("name".equals(type)){
                List<String> nameList = receivingStationList.stream().map(SpBoatsReceivingStation::getName).filter(name -> name != null && !name.isEmpty()).distinct().collect(Collectors.toList());
                return nameList;
            }else if ("startup".equals(type)){
                List<String> startupList = receivingStationList.stream().map(SpBoatsReceivingStation::getStartup).filter(startup -> startup != null && !startup.isEmpty()).distinct().collect(Collectors.toList());
                return startupList;
            }else if ("status".equals(type)){
                List<String> statusList = receivingStationList.stream().map(SpBoatsReceivingStation::getStatus).filter(status -> status != null && !status.isEmpty()).distinct().collect(Collectors.toList());
                return statusList;
            }
        }
        return null;
    }

    @Override
    public QueryBoatsReceivingStationInfo queryBoatsReceivintStationById(String id) {
        QueryBoatsReceivingStationInfo info = baseMapper.queryBoatsReceivintStationById(id);
        if (null != info){
            SpBoatsWeather boatsWeather = baseMapper.queryBoatsWeather(info.getLat(),info.getLon());
            info.setBoatsWeather(boatsWeather);
        }
        return info;
    }

    @Override
    public String queryReceivingStationName(String id) {
        SpBoatsReceivingStation spBoatsReceivingStation = baseMapper.selectById(id);
        return spBoatsReceivingStation.getName();
    }

    @Override
    public List<SpBoatsReceivingStation> exportReceivingStation(QueryReceivStationRq rq) {
        List<SpBoatsReceivingStation> boatsList = baseMapper.queryBoatsReceivingStationList(rq);
        return boatsList;
    }

    @Override
    @Transactional
    public boolean createReceivintStation(List<SpBoatsReceivingStation> receivingStationList) {
        if (CollectionUtil.isNotEmpty(receivingStationList)){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("enable","1");
            int delete = baseMapper.delete(queryWrapper);
            List<SavePortInfoDto> portInfoDtos = new ArrayList<>();
            receivingStationList.forEach(item -> {
                item.setEnable("1");
                item.setCreateTime(new Date());
                item.setUpdateTime(new Date());
                SavePortInfoDto savePortInfoDto = new SavePortInfoDto();
                savePortInfoDto.setPortNameEnglish(item.getName());
                savePortInfoDto.setPortCountry(item.getCountry());
                savePortInfoDto.setBelongPortNameEnglish(item.getPort());
                savePortInfoDto.setLon(item.getLon());
                savePortInfoDto.setLat(item.getLat());
                portInfoDtos.add(savePortInfoDto);
            });
            portInformationService.savePortInfoList(portInfoDtos,2);
            boolean b = this.saveBatch(receivingStationList);
            return b;
        }
        return false;
    }
}
